package com.tsingtree.modular.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsingtree.modular.entity.NanFeedback;
import com.tsingtree.modular.entity.NanSeller;
import com.tsingtree.modular.mapper.NanFeedbackMapper;
import com.tsingtree.modular.model.params.NanFeedbackParam;
import com.tsingtree.modular.model.result.NanFeedbackResult;
import com.tsingtree.modular.model.result.NanSellAnnounceResult;
import  com.tsingtree.modular.service.NanFeedbackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsingtree.modular.service.NanSellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import com.tsingtree.base.pojo.page.LayuiPageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tsingtree.base.pojo.page.LayuiPageFactory;
import com.tsingtree.roses.core.util.ToolUtil;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 用户反馈 服务实现类
 * </p>
 *
 * @author jagger
 * @since 2024-06-03
 */
@Service
public class NanFeedbackServiceImpl extends ServiceImpl<NanFeedbackMapper, NanFeedback> implements NanFeedbackService {
    @Autowired
    private NanSellerService nanSellerService;

    @Override
    public void add(NanFeedbackParam param){
        NanFeedback entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(NanFeedbackParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(NanFeedbackParam param){
        NanFeedback oldEntity = getOldEntity(param);
        NanFeedback newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public NanFeedbackResult findBySpec(NanFeedbackParam param){
        return null;
    }

    @Override
    public List<NanFeedbackResult> findListBySpec(NanFeedbackParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(NanFeedbackParam param){
        Page pageContext = getPageContext();
        IPage<NanFeedbackResult> page = this.baseMapper.customPageList(pageContext, param);
        List<Integer> collect = page.getRecords().stream().map(NanFeedbackResult::getSellerId).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(collect)){
            List<NanSeller> nanSellers = nanSellerService.list(new QueryWrapper<NanSeller>().in("id", collect));
            Map<Integer, String> map = nanSellers.stream().collect(Collectors.toMap(NanSeller::getId, NanSeller::getName));
            page.getRecords().forEach(result -> {
                result.setSellerName(map.get(result.getSellerId()));
            });
        }
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(NanFeedbackParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private NanFeedback getOldEntity(NanFeedbackParam param) {
        return this.getById(getKey(param));
    }

    private NanFeedback getEntity(NanFeedbackParam param) {
        NanFeedback entity = new NanFeedback();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
